<template>
  <el-dialog title="分配权限" :visible="isShow" @close="closeEvent">
    <el-tree
      ref="tree"
      :data="treeData"
      :props="{ label: 'name' }"
      :default-checked-keys="permIds"
      show-checkbox
      node-key="id"
      check-strictly
    />
    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { changeData } from '@/utils/index.js'
import { sysPermission } from '@/api/permission.js'
import { getSysRole, updateRole } from '@/api/setting.js'
export default {
  name: 'DialogPower',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      permIds: [],
      info: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    closeEvent() {
      this.$emit('update:isShow', false)
      // 清空默认勾选
      this.$refs.tree.setCheckedKeys([])
    },
    async getData() {
      // 获取角色权限点
      const res = await sysPermission()
      //   console.log(res)
      this.treeData = changeData(res.data, '0')
    },
    // 获取角色详情
    async getRoleInfo(id) {
      const res = await getSysRole(id)
      this.info = res.data
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log(res.data)
    },
    // 确定点击
    async submit() {
      await updateRole({
        ...this.info,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('更新权限成功！')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
